﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DCT.CodeBox.LightLog.Codes;
using DCT.CodeBox.Log4netWrapper;
using DCT.CodeBox.LightLog.Codes.Formatter;

namespace DCT.CodeBox.Log4netWrapper.LightLogExtension.Codes
{
    public class LightLog4Net : ILog
    {
        log4net.ILog log4netILog = null;

        public object LoadConfig(object log)
        {
            this.log4netILog = log as log4net.ILog;
            return log;
        }

        public void D(string tag, object msg)
        {
            this.log4netILog.Debug(this.FormatLogString(tag, msg));
        }

        public void D(string tag, object msg, Exception exception)
        {
            this.log4netILog.Debug(this.FormatLogString(tag, msg), exception);
        }

        public void E(string tag, object msg)
        {
            this.log4netILog.Error(this.FormatLogString(tag, msg));
        }

        public void E(string tag, object msg, Exception exception)
        {
            this.log4netILog.Error(this.FormatLogString(tag, msg), exception);
        }

        public void F(string tag, object msg)
        {
            this.log4netILog.Fatal(this.FormatLogString(tag, msg));
        }

        public void F(string tag, object msg, Exception exception)
        {
            this.log4netILog.Fatal(this.FormatLogString(tag, msg), exception);
        }

        public void I(string tag, object msg)
        {
            this.log4netILog.Info(this.FormatLogString(tag, msg));
        }

        public void I(string tag, object msg, Exception exception)
        {
            this.log4netILog.Info(this.FormatLogString(tag, msg), exception);
        }

        public void W(string tag, object msg)
        {
            this.log4netILog.Warn(this.FormatLogString(tag, msg));
        }

        public void W(string tag, object msg, Exception exception)
        {
            this.log4netILog.Warn(this.FormatLogString(tag, msg), exception);
        }

        public void Log(LogLevel level, string tag, object msg)
        {
            this.Log(level, tag, msg, null);
        }

        public void Log(LogLevel level, string tag, object msg, Exception exception)
        {
            string msgInString = this.FormatLogString(tag, msg);
            switch (level)
            {
                case LogLevel.Debug:
                    {
                        this.log4netILog.Debug(msgInString, exception);
                        break;
                    }
                case LogLevel.Info:
                    {
                        this.log4netILog.Info(msgInString, exception);
                        break;
                    }
                case LogLevel.Error:
                    {
                        this.log4netILog.Error(msgInString, exception);
                        break;
                    }
                case LogLevel.Warning:
                    {
                        this.log4netILog.Warn(msgInString, exception);
                        break;
                    }
                case LogLevel.Fatal:
                    {
                        this.log4netILog.Fatal(msgInString, exception);
                        break;
                    }
            }
        }

        public bool EnabledLog(LogLevel level)
        {
            switch (level)
            {
                case LogLevel.Debug:
                    {
                        return this.log4netILog.IsDebugEnabled;
                    }
                case LogLevel.Info:
                    {
                        return this.log4netILog.IsInfoEnabled;
                    }
                case LogLevel.Error:
                    {
                        return this.log4netILog.IsErrorEnabled;
                    }
                case LogLevel.Warning:
                    {
                        return this.log4netILog.IsWarnEnabled;
                    }
                case LogLevel.Fatal:
                    {
                        return this.log4netILog.IsFatalEnabled;
                    }
            }
            return false;   //不可能到这一步，因为上面已经枚举了所有情况
        }

        protected virtual string FormatLogString(string tag, object msg)
        {
            string format = "[" + tag + "]"
                + LogStringFormatter.Render(msg);
            return format;
        }

        public log4net.ILog Log4netILog {
            get { 
                return log4netILog;
            }
        }
    }
}
